package com.wangy.sct.serviceuser.app.service;

import com.wangy.sct.common.core.SctEnum;
import com.wangy.sct.common.core.SctRuntimeException;
import com.wangy.sct.serviceuser.api.model.response.UserServiceResp;
import com.wangy.sct.serviceuser.app.mapper.UserInfoMapper;
import com.wangy.sct.serviceuser.app.schema.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: UserService <br/>
 * Description:  <br/>
 *
 * @author wangyue
 * @version V1.0.0
 * @date 2018/11/5 6:23 PM
 */
@Slf4j
@Service
public class UserService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	/**
	 * @Description:
	 * @param: [userId]
	 * @return: com.wangy.sct.serviceuser.schema.dto.response.UserInfoResp
	 * @author: wangyue
	 * @Date: 2018/11/5 6:37 PM
	 */
	@Cacheable(cacheNames = "USER_INFO", key = "#userId", sync = true)
	public UserServiceResp queryById(String userId) {
		UserServiceResp userInfoResp = null;
		try {
			userInfoResp = userInfoMapper.findById(userId);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("根据userId查询用户", e);
			throw new SctRuntimeException(SctEnum.UNKNOWEN_EXCEPTION, e);
		}
		if (null == userInfoResp) {
			throw new SctRuntimeException(SctEnum.USER_NOT_EXIST);
		}
		return userInfoResp;
	}

	/**
	 * @Description:
	 * @param: [userName, passWord]
	 * @return: com.wangy.sct.serviceuser.schema.dto.response.UserInfoResp
	 * @author: wangyue
	 * @Date: 2018/11/5 8:17 PM
	 */
	public UserServiceResp queryByNameAndPwd(String userName, String passWord) {
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName);
		userInfo.setPassWord(passWord);
		List<UserServiceResp> userInfoRespList = null;
		try {
			userInfoRespList = userInfoMapper.select(userInfo);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("根据userName，passWord查询用户", e);
			throw new SctRuntimeException(SctEnum.UNKNOWEN_EXCEPTION);
		}
		if (null != userInfoRespList && ! userInfoRespList.isEmpty()) {
			return userInfoRespList.get(0);
		} else {
			throw new SctRuntimeException(SctEnum.USER_NOT_EXIST);
		}

	}


}
